/*
 * GuiFactoryImpl.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/**
 * <p>
 * GuiFactoryImpl
 * </p>.
 * 
 * @author Cesar
 */
public class GuiFactoryImpl extends GuiFactory {

	/** The _this. */
	private static GuiFactoryImpl _this;

	/** The _midlet. */
	private MIDlet _midlet;

	/**
	 * Creates a new instance of GuiFactoryImpl.
	 */
	public GuiFactoryImpl() {
	}

	/**
	 * Gets the midlet.
	 * 
	 * @return the midlet
	 */
	public MIDlet getMidlet() {
		return _midlet;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.gui.GuiFactory#setMidlet(javax.microedition.midlet.MIDlet)
	 */
	public void setMidlet(MIDlet aMidlet) {
		_midlet = aMidlet;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.gui.GuiFactory#displayMessageBox(java.lang.String,
	 *      java.lang.String)
	 */
	public MessageBox displayMessageBox(String title, String msg) {
		return new MessageBoxImpl(title, msg);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.gui.GuiFactory#makeConsole(org.scriptme.ecmascript.gui.InterpreterCommands,
	 *      java.lang.String, int, int)
	 */
	public Console makeConsole(InterpreterCommands itrpParam, String title,
			int rows, int columns) {
		System.out.println("[CONSOLE_IMLP]::NOT YET IMPLEMENTED::" + title);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.gui.GuiFactory#prompt(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public PromptBox prompt(String title, String prompt, String defaultResponse) {
		return new PromptBoxImpl(title, prompt, defaultResponse);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.gui.GuiFactory#confirm(java.lang.String,
	 *      java.lang.String)
	 */
	public ConfirmationBox confirm(String title, String msg) {
		return new ConfirmationBoxImpl(title, msg);
	}

	/**
	 * MessageBoxImpl - Implementation to create simple UI components.
	 */
	private class MessageBoxImpl implements MessageBox {

		/** The title. */
		private String title;

		/** The message. */
		private String message;

		/**
		 * Instantiates a new message box impl.
		 * 
		 * @param title
		 *            the title
		 * @param message
		 *            the message
		 */
		public MessageBoxImpl(String title, String message) {
			this.title = title;
			this.message = message;
		}

		/**
		 * Show.
		 */
		private void show() {
			Alert alert = new Alert(title, message, null, AlertType.INFO);

			Display.getDisplay(getMidlet()).setCurrent(alert);

			System.out.println("[MESSAGEBOX_IMLP]::" + this.title + " - "
					+ this.message);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.scriptme.ecmascript.gui.MessageBox#waitOK()
		 */
		public synchronized void waitOK() {
			show();
		}
	}

	/**
	 * Implementation of the prompt box.
	 */
	private class PromptBoxImpl implements PromptBox {

		/** The response. */
		private String response = null;

		/** The title. */
		private String title;

		/** The prompt. */
		private String prompt;

		/** The default response. */
		private String defaultResponse;

		/**
		 * Create a PromptBox with default response.
		 * 
		 * @param title
		 *            The window title
		 * @param prompt
		 *            The prompt string
		 * @param defaultResponse
		 *            The default response (will be "" if null)
		 */
		public PromptBoxImpl(String title, String prompt, String defaultResponse) {
			this.title = title;
			this.prompt = prompt;
			this.defaultResponse = defaultResponse;
		}

		/**
		 * Show.
		 */
		private void show() {
			System.out.println("[PROMTBOX_IMLP]::" + this.title + " - "
					+ this.prompt);

			if (response == null) {
				response = defaultResponse == null ? "" : defaultResponse;
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.scriptme.ecmascript.gui.PromptBox#waitResponse()
		 */
		public String waitResponse() {
			show();
			return response;
		}
	}

	/**
	 * Implementation of a confirmation box.
	 */
	private class ConfirmationBoxImpl implements ConfirmationBox {

		/** The response. */
		private boolean response = false;

		/** The title. */
		private String title;

		/** The message. */
		private String message;

		/**
		 * Create a confirmation box.
		 * 
		 * @param title
		 *            The title string
		 * @param message
		 *            The message text
		 */
		public ConfirmationBoxImpl(String title, String message) {
			this.title = title;
			this.message = message;
		}

		/**
		 * Show.
		 */
		private void show() {
			System.out.println("[CONFIRMATIONBOX_IMLP]::" + this.title + " - "
					+ this.message);
		}

		/**
		 * Wait for the answer and return it.
		 * 
		 * @return the answer as a boolean
		 */
		public boolean waitYesOrNo() {
			show();
			return response;
		}
	}
}
